package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.utils.QiniuUtils;
import com.itheima.constant.RedisConstant;
import com.itheima.pojo.QueryPageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/setMeal")
public class SetMealController {
    //获取service层对象
    @Reference
    private SetMealService setMealService;

    //获取Jedis对象
    @Autowired
    private JedisPool jedisPool;

    /**
     * 分页查询套餐
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findSetMealByPage.do")
    public Result findSetMealByPage(@RequestBody QueryPageBean queryPageBean){
        Result result = setMealService.findSetMealByPage(queryPageBean);
        return result;
    }

    /**
     * 将图片上传到七牛云上面
     * @param file
     * @return
     */
    @RequestMapping("/upload.do")
    public Result upload(@RequestParam("imgFile") MultipartFile file){
        try {
            if (!file.isEmpty()){
                //上传的文件不为空,获取文件名字
                String originalFilename = file.getOriginalFilename();
                //获取后缀名
                int index = originalFilename.indexOf(".");
                String substring = originalFilename.substring(index);
                //生成一个唯一的文件名
                String fileName = UUID.randomUUID()+substring;
                //调用工具类,将图片上传到七牛云上面
                QiniuUtils.upload2Qiniu(file.getBytes(),fileName);

                //将自动上传到七牛云的图片存到redis中
                Jedis resource = jedisPool.getResource();
                resource.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
                resource.close();
                return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
            }else{
                //文件为空,上传失败
                return new Result(false,MessageConstant.UPLOAD_FAIL);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 添加套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @RequestMapping("/addSetMeal.do")
    public Result addSetMeal(@RequestParam("checkgroupIds") int[] checkgroupIds, @RequestBody Setmeal setmeal){
        Result result = setMealService.addSetMeal(checkgroupIds,setmeal);
        return result;
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @RequestMapping("/findSetMealById.do")
    public Result findSetMealById(@RequestParam String id){
        //调用分页查询的方法
        Result result = setMealService.findSetMealById(id);
        return result;
    }

    /**
     * 根据套餐id查询与检查组的关联
     * @param id
     * @return
     */
    @RequestMapping("/findCheckGroupIdsBySetMealId.do")
    public Result findCheckGroupIdsBySetMealId(@RequestParam String id){
        Result result = setMealService.findCheckGroupIdsBySetMealId(id);
        return result;
    }

    /**
     * 编辑套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @RequestMapping("/updateSetMealById.do")
    public Result updateSetMealById(@RequestParam("checkgroupIds") int[] checkgroupIds, @RequestBody Setmeal setmeal){
        Result result = setMealService.updateSetMealById(checkgroupIds,setmeal);
        return result;
    }

    /**
     * 删除套餐
     * @param id
     * @return
     */
    @RequestMapping("/deleteSetMealById.do")
    public Result deleteSetMealById(@RequestParam String id){
        Result result = setMealService.deleteSetMealById(id);
        return result;
    }

}
